package com.oritsh.dicom.wado.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.oritsh.dicom.exception.DicomException;
import com.oritsh.dicom.service.CFindService;
import org.dcm4che.data.Attributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 * User: zarra
 * Date: 14-2-5
 * Time: 下午4:44
 * Copyright Shanghai Orient Rain Information Technology Co.,Ltd.
 */

@RequestMapping("/cfind")
@Controller
public class CFindController {

    @Autowired
    CFindService cFindService;

    Logger logger = LoggerFactory.getLogger(CFindController.class);

    @RequestMapping(value="",
            method = {RequestMethod.POST},
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Attributes[] rawCFind(@RequestBody Attributes requestDataset)
            throws DicomException, JsonProcessingException {
        return cFindService.rawFind(requestDataset);
    }


    @RequestMapping(value="",
            method = {RequestMethod.GET},
            produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView rootPage() {
        return new ModelAndView("cfind");
    }

    @RequestMapping(value="/patientID/{id}",
            method = {RequestMethod.GET,RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Attributes[] findByPatientID(@PathVariable("id")String patientid)
            throws DicomException{
        return cFindService.findDataSetByPatientID(patientid);
    }

    @RequestMapping(value="/study/{uid:.+}",
            method = {RequestMethod.GET,RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Attributes[] findByStudyUID(@PathVariable("uid")String studyUID)
            throws DicomException{
        logger.info("Get studyID:"+studyUID);
        return cFindService.findAllImageByStudyUID(null, studyUID);
    }

    @RequestMapping(value="/date/{date}",
            method = {RequestMethod.GET,RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Attributes[] findByStudyDate(@PathVariable("date")String date)
            throws DicomException{
        logger.info("Get studydate:"+date);
        return cFindService.findDataSetByStudyDate(date);
    }
}
